package com.googlecode.animator.view.timeline
{
	import com.googlecode.animator.data.Notifications;
	import com.googlecode.animator.data.objects3d.Object3DVO;
	import com.googlecode.animator.model.ProjectSettingsProxy;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class InfoPaneMediator extends Mediator
	{
		/**
		 * Identifier. 
		 */
		public static const NAME:String = "infoPaneMediator";
		
		/**
		 * Constructor. 
		 * @param viewComponent
		 * 
		 */
		public function InfoPaneMediator(viewComponent:InfoPane)
		{
			super(NAME, viewComponent);
		}
		
		/**
		 * List notification of interests. 
		 * @return 
		 * 
		 */
		override public function listNotificationInterests():Array
		{
			var list:Array = super.listNotificationInterests();
			
			list.push(Notifications.OBJECT3D_SELECTED);
			list.push(Notifications.OBJECT3D_DELETED);
			list.push(Notifications.OBJECT3D_LABEL_CHANGED);
			
			return list;
			
		}
		
		/**
		 * Handle notifications. 
		 * @param notification
		 * 
		 */
		override public function handleNotification(notification:INotification):void
		{
			super.handleNotification(notification);
			
			switch(notification.getName())
			{
				case Notifications.OBJECT3D_LABEL_CHANGED:
				
					handleObject3DLabelChanged(notification.getBody() as String);
					
					break;
					
				case Notifications.OBJECT3D_SELECTED:
				
					handleObject3DSelected(notification.getBody() as String);
					
					break;
					
				case Notifications.OBJECT3D_DELETED:
				
					handleObject3DDeleted();
					
					break;
			}
		}
		
		/**
		 * Respond to a user selecting an object3D. 
		 * @param name
		 * 
		 */
		protected function handleObject3DSelected(name:String):void
		{
			var VO:Object3DVO = settings.getObject3D(name);
			
			if(VO.label == null || VO.label == "")
			{
				viewRef.objTxt = VO.name;
			}else
			{
				viewRef.objTxt = VO.label;
			}
			
		}
		
		/**
		 * Respond to a user changing an object3D label. 
		 * @param name
		 * 
		 */
		protected function handleObject3DLabelChanged(name:String):void
		{
			var VO:Object3DVO = settings.getObject3D(name);
			viewRef.objTxt = VO.label;			
		}
		
		/**
		 * Respond to a user deleting an object3D. 
		 * 
		 */
		protected function handleObject3DDeleted():void
		{
			viewRef.objTxt = "Nothing selected";
		}
		
		/**
		 * Returns typed reference to ProjectSettingsProxy. 
		 * @return 
		 * 
		 */
		protected function get settings():ProjectSettingsProxy
		{
			var proxy:IProxy = facade.retrieveProxy(ProjectSettingsProxy.NAME) as IProxy;
			
			return proxy as ProjectSettingsProxy;
		}
		
		/**
		 * Returns typed reference to component. 
		 * @return 
		 * 
		 */
		protected function get viewRef():InfoPane
		{
			return viewComponent as InfoPane;
		}
	}
}